Imports System
Imports System.ComponentModel
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Text
Imports System.Windows.Forms

Namespace xpComponent.Form
    Public Module IconsMenuMain
        Dim m_Font As New System.Drawing.Font("Arial", 8)
        Sub MeasureItems(ByVal EvMeasureItem As System.Windows.Forms.MeasureItemEventArgs, ByVal Mi As System.Windows.Forms.MenuItem)
            Dim sf As System.Drawing.StringFormat = New System.Drawing.StringFormat
            sf.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.Show
            sf.SetTabStops(60, New Single() {0})
            EvMeasureItem.ItemHeight = 22
            EvMeasureItem.ItemWidth = CInt(EvMeasureItem.Graphics.MeasureString(GetRealText(Mi), m_Font, 10000, sf).Width) + 10
            sf.Dispose()
            sf = Nothing
        End Sub

        Sub DrawItems(ByVal EvDrawItems As System.Windows.Forms.DrawItemEventArgs, ByVal Mi As System.Windows.Forms.MenuItem, ByVal m_Icon As System.Drawing.Icon)
            Dim br As System.Drawing.Brush
            Dim fDisposeBrush As Boolean
            If Not m_Icon Is Nothing Then
                If Not Mi.Checked Then
                    EvDrawItems.Graphics.DrawIcon(m_Icon, EvDrawItems.Bounds.Left + 2, EvDrawItems.Bounds.Top + 2)
                Else
                    EvDrawItems.Graphics.DrawIcon(m_Icon, EvDrawItems.Bounds.Left + 2, EvDrawItems.Bounds.Top + 2)
                    Dim nPen As System.Drawing.Pen
                    If Not Mi.Enabled Then
                        NPEN = New System.Drawing.Pen(System.Drawing.Color.DarkGray)
                    Else
                        nPen = New System.Drawing.Pen(System.Drawing.Color.Gray)
                    End If
                    EvDrawItems.Graphics.DrawRectangle(nPen, 1, EvDrawItems.Bounds.Top, 20, 20)
                    EvDrawItems.Graphics.DrawRectangle(nPen, 3, EvDrawItems.Bounds.Top + 2, 16, 16)
                End If
            Else
                If Mi.Checked Then
                    Dim nPen As System.Drawing.Pen
                    If Not Mi.Enabled Then
                        NPEN = New System.Drawing.Pen(System.Drawing.Color.DarkGray)
                    Else
                        nPen = New System.Drawing.Pen(System.Drawing.Color.Gray)
                    End If
                    EvDrawItems.Graphics.DrawRectangle(nPen, 1, EvDrawItems.Bounds.Top, 20, 20)
                    Dim Pnts() As System.Drawing.Point
                    ReDim Pnts(2)
                    Pnts(0) = New System.Drawing.Point(15, EvDrawItems.Bounds.Top + 6)
                    Pnts(1) = New System.Drawing.Point(8, EvDrawItems.Bounds.Top + 13)
                    Pnts(2) = New System.Drawing.Point(5, EvDrawItems.Bounds.Top + 10)
                    If Mi.Enabled Then
                        EvDrawItems.Graphics.DrawLines(New System.Drawing.Pen(System.Drawing.Color.Black), Pnts)
                    Else
                        EvDrawItems.Graphics.DrawLines(New System.Drawing.Pen(System.Drawing.Color.Gray), Pnts)
                    End If
                End If
            End If
            Dim rcBk As System.Drawing.Rectangle = EvDrawItems.Bounds
            rcBk.X += 24

            If CBool(EvDrawItems.State And System.Windows.Forms.DrawItemState.Selected) Then
                br = New LinearGradientBrush(rcBk, System.Drawing.Color.MidnightBlue, System.Drawing.Color.LightBlue, 0)
                fDisposeBrush = True
            Else
                br = System.Drawing.SystemBrushes.Control
            End If
            EvDrawItems.Graphics.FillRectangle(br, rcBk)
            If fDisposeBrush Then br.Dispose()
            br = Nothing
            Dim sf As System.Drawing.StringFormat = New System.Drawing.StringFormat
            sf.HotkeyPrefix = HotkeyPrefix.Show
            sf.SetTabStops(60, New Single() {0})
            If Mi.Enabled Then
                br = New System.Drawing.SolidBrush(EvDrawItems.ForeColor)
            Else
                br = New System.Drawing.SolidBrush(System.Drawing.Color.Gray)
            End If
            EvDrawItems.Graphics.DrawString(GetRealText(Mi), m_Font, br, EvDrawItems.Bounds.Left + 25, EvDrawItems.Bounds.Top + 2, sf)
            br.Dispose()
            br = Nothing
            sf.Dispose()
            sf = Nothing
        End Sub

        Function GetRealText(ByVal Mi As System.Windows.Forms.MenuItem) As String
            Dim s As String = Mi.Text
            If Mi.ShowShortcut And Mi.Shortcut <> System.Windows.Forms.Shortcut.None Then
                Dim k As System.Windows.Forms.Keys = CType(Mi.Shortcut, System.Windows.Forms.Keys)
                s = s & Convert.ToChar(9) & TypeDescriptor.GetConverter(GetType(System.Windows.Forms.Keys)).ConvertToString(k)
            End If
            Return s
        End Function
    End Module
End Namespace